using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;
using System.Reflection;
using FreeSMS.Configuration.Sections;

namespace FreeSMS.Configuration
{
    public class UpdateInfo
    {
        #region Static Members
        internal static readonly UpdateInfo Current = new UpdateInfo(Core.ProgramFullVersion, "Current version");
        #endregion

        #region Static Methods
        public static UpdateInfo LoadUpdateInfo(Stream stream)
        {
            UpdateInfo updateInfo = null;

            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(UpdateInfo));
                StreamReader sr = new StreamReader(stream);
                updateInfo = (UpdateInfo)ser.Deserialize(sr);
                sr.Close();
            }
            catch
            {

            }

            return updateInfo;

        }
        #endregion

        #region Members
        private string _version;
        private string _comments;
        #endregion

        #region Properties
        public string Version
        {
            get { return _version; }
            set { _version = value; }
        }	
        public string Comments
        {
            get { return _comments; }
            set { _comments = value; }
        }
        #endregion
	                       
        #region Methods
        public void SaveUpdateInfo(string updateInfoFile)
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(UpdateInfo));
                StreamWriter sw = new StreamWriter(updateInfoFile);
                ser.Serialize(sw, this);
                sw.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        #endregion       

        #region Initialize
        public UpdateInfo(string version, string comments)
        {
            _version = version;
            _comments = comments;
        }
        public UpdateInfo()
            : this(string.Empty, string.Empty)
        {

        }
        #endregion

        public static bool operator > (UpdateInfo updateInfo1, UpdateInfo updateInfo2)
        {
            string[] array1 = updateInfo1._version.Split('.');
            string[] array2 = updateInfo2._version.Split('.');

            for (int i = 0; i < array1.Length; i++)            
                if (int.Parse(array1[i]) > int.Parse(array2[i]))
                    return true;            

            return false;
        }

        public static bool operator <(UpdateInfo updateinfo1, UpdateInfo updateInfo2)
        {
            // hurd
            return false;
        }
    }    
}
